package com.xuexiang.xuidemo.page.uilts;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentState;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Switch;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.BaseDialog;
import ohos.agp.window.dialog.CommonDialog;

import com.xuexiang.xui_lib.util.StatusBarUtils;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.page.base.BasePageAbilitySlice;
import com.xuexiang.xuidemo.page.base.Page;
import com.xuexiang.xuidemo.util.DisplayUtils;
import com.xuexiang.xuidemo.util.JumpUtil;

/**
 * StatusBarUtils示例页面
 *
 * @since 2021-03-24
 */
@Page(name = "StatusBarUtils", action = "StatusBarUtils")
public class StatusBarUtilsAbilitySlice extends BasePageAbilitySlice implements Component.ClickedListener {
    private static final int COLOR_WHITE = 0xFFFFFFFF;
    private static final int COLOR_TRACK_ON = 0xFF87CEFA;
    private static final int COLOR_TRACK_OFF = 0xFF808080;
    private static final int RADIUS = 50;
    private static final int INT_10 = 10;
    private Switch switchFullScreen;

    @Override
    protected void onActive() {
        super.onActive();
        findComponentById(ResourceTable.Id_tv_translucent).setClickedListener(this);
        findComponentById(ResourceTable.Id_tv_dialog).setClickedListener(this);
        switchFullScreen = findCastComponentById(ResourceTable.Id_switch_fullScreen);
        initSwitch();
    }

    private void initSwitch() {
        ShapeElement elementThumbOn = new ShapeElement();
        elementThumbOn.setShape(ShapeElement.OVAL);
        elementThumbOn.setRgbColor(RgbColor.fromArgbInt(COLOR_WHITE));
        elementThumbOn.setCornerRadius(RADIUS);
        ShapeElement elementThumbOff = new ShapeElement();
        elementThumbOff.setShape(ShapeElement.OVAL);
        elementThumbOff.setRgbColor(RgbColor.fromArgbInt(COLOR_WHITE));
        elementThumbOff.setCornerRadius(RADIUS);
        ShapeElement elementTrackOn = new ShapeElement();
        elementTrackOn.setShape(ShapeElement.RECTANGLE);
        elementTrackOn.setRgbColor(RgbColor.fromArgbInt(COLOR_TRACK_ON));
        elementTrackOn.setCornerRadius(RADIUS);
        ShapeElement elementTrackOff = new ShapeElement();
        elementTrackOff.setShape(ShapeElement.RECTANGLE);
        elementTrackOff.setRgbColor(RgbColor.fromArgbInt(COLOR_TRACK_OFF));
        elementTrackOff.setCornerRadius(RADIUS);

        switchFullScreen.setTrackElement(trackElementInit(elementTrackOn, elementTrackOff));
        switchFullScreen.setThumbElement(thumbElementInit(elementThumbOn, elementThumbOff));

        // 回调处理Switch状态改变事件
        switchFullScreen.setCheckedStateChangedListener((button, isChecked) -> {
            if (isChecked) {
                StatusBarUtils.fullScreen();
            } else {
                StatusBarUtils.cancelFullScreen();
            }
        });
    }

    private StateElement trackElementInit(ShapeElement on, ShapeElement off) {
        StateElement trackElement = new StateElement();
        trackElement.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, on);
        trackElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, off);
        return trackElement;
    }

    private StateElement thumbElementInit(ShapeElement on, ShapeElement off) {
        StateElement thumbElement = new StateElement();
        thumbElement.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, on);
        thumbElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, off);
        return thumbElement;
    }

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_ability_utils_statusbar;
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_tv_translucent:
                JumpUtil.jumpToAbility(getContext(), TranslucentAbility.class);
                break;
            case ResourceTable.Id_tv_dialog:
                DirectionalLayout itemView = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                    .parse(ResourceTable.Layout_dialog_utils_full_screen_test, null, false);
                BaseDialog baseDialog = new CommonDialog(getContext())
                    .setContentCustomComponent(itemView)
                    .setAutoClosable(true)
                    .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                    .setAlignment(LayoutAlignment.CENTER)
                    .setCornerRadius(DisplayUtils.vp2px(getContext(), INT_10));
                itemView.findComponentById(ResourceTable.Id_tv_ok)
                    .setClickedListener(component1 -> baseDialog.hide());
                baseDialog.show();
                break;
            default:
                // ignore
        }
    }
}

